﻿//-----------------------------------------------------------------------
// <copyright file="ActionNameBinder.cs" company="Sławomir Rosiek">
//     Copyright (c) Sławomir Rosiek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Rosieks.Web.Flow
{
    using System.Web.Mvc;

    public class ActionNameBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var values = bindingContext.ValueProvider.GetValue("ActionName");
            if (values != null)
            {
                foreach (string action in (string[])values.RawValue)
                {
                    if (bindingContext.ValueProvider.GetValue(string.Format("ActionName.{0}", action)) != null ||
                       (bindingContext.ValueProvider.GetValue(string.Format("ActionName.{0}.x", action)) != null &&
                        bindingContext.ValueProvider.GetValue(string.Format("ActionName.{0}.y", action)) != null))
                    {
                        return action;
                    }
                }
            }

            return null;
        }
    }
}
